<?php 
class Exam_Model_DbTable_Useranswers extends Engine_Db_Table{
    protected $_rowClass = 'Exam_Model_Useranswer';
    
    public function getStaticQuestionAnswer($exam_id =0 ,$question_id = 0){
        $select = $this->select()
                       ->from($this->info('name'),array('*',new Zend_Db_Expr(' count(*) as total_choice ')));
        if(!empty($exam_id)){
            $select->where('exam_id = ?',$exam_id);
        } 
        if(!empty($question_id)){
            if(is_numeric($question_id)){
                $select->where('question_id = ?',$question_id);
            }
            else {
                if(is_array($question_id)&& !empty($question_id)){
                    $select->where('question_id in (?)',$question_id);
                }
            }
            
        }
        
        $select->order('question_id');
        $select->group('group');
        
        $rows = $this->fetchAll($select);
        if(!$rows) return null;
        
        $result = array();
        foreach($rows as $item){
            
            if(!isset($result[$item->question_id])){
                $result[$item->question_id] = array($item->group => 0);  
            }
            if(!isset($result[$item->question_id][$item->group])) $result[$item->question_id][$item->group] = 0;
            $result[$item->question_id][$item->group] +=  $item->total_choice;
        } 
        return $result;             
    }
    
    public function insertAnswer($answers,$user_id,$exam_id,$test_id){
        $columns = '(question_id,user_id,answer_id,exam_id,test_id,`group`)';
        $values = array();
        foreach($answers as $question_id=>$answer){
            $t = '';
            $group = '';
            foreach($answer as $answer_id){
                $group .= $answer_id.',';
            }
            foreach($answer as $answer_id){
                $t = "($question_id,$user_id,$answer_id,$exam_id,$test_id,'$group')";
            }
            $values[] = $t;
        }
        if(count($values)<1) return;
        $sql = ' insert into engine4_exam_useranswers '.$columns.' values'.implode(',',$values) ;
        $stmt = $this->getAdapter()->prepare($sql);
        $stmt->execute();
    }
    
    public function getQuestion($test_id,$type = Exam_Model_Answer::Exam_Answer_All){
        $qT = Engine_Api::_()->getDbTable('questions','exam');
        $qN = $qT->info('name');
        
        $sl = $qT->select()
                 ->form(array('questions'=>$qN),'*')
                 ->join(array('useranswers'=>$this->info('name')),
                    "questions.question_id  = useranswers.question_id ",
                    null
                 )
                 ->where('useranswers.test_id = ?',$test_id)
                 ->group('useranswers.question_id');
                 
        if($type == Exam_Model_Answer::Exam_Answer_False){
            $sl->where('useranswers.correct = 0');
        }
        if($type == Exam_Model_Answer::Exam_Answer_True){
            $sl->where('useranswers.correct = 1');
        }
        
        return $qT->fetchAll($sl);
    }
    
    public function getUserAnswer($test_id,$user_id){
        $sl = $this->select()
                   ->where('user_id = ?',$user_id)
                   ->where('test_id = ?',$test_id);
        $rows = $this->fetchAll($sl);
        $result = array();
        foreach($rows as $item){
            $result[$item->question_id][] = $item->answer_id;
        }
        return $result;
    }
    
    
}
    
?>